package com.freesky.biz.rights.controller;

import com.alipay.v3.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.RightsActivityEntity;
import com.freesky.biz.rights.domain.RightsActivityRuleEntity;
import com.freesky.biz.rights.service.IRightsActivityRuleService;
import com.freesky.biz.rights.service.IRightsActivityService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.QueryWrapperUtils;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 权益活动规则管理
 */
@RestController
@RequestMapping("/rights/activity/rule")
public class RightsActivityRuleController extends BaseController {

    @Autowired
    private IRightsActivityRuleService rightsActivityRuleService;

    /**
     * 分页获取权益规则活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activityrule:list')")
    @GetMapping("/page")
    public TableDataInfo page(RightsActivityRuleEntity rightsActivity) {
        QueryWrapper<RightsActivityRuleEntity> queryWrapper = QueryWrapperUtils.toSimpleQuery(rightsActivity);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByAsc("irasno");
        IPage<RightsActivityRuleEntity> pageList = rightsActivityRuleService.page(plusPage(),queryWrapper);
        return getDataTable(pageList);
    }

    @PreAuthorize("@ss.hasPermi('rights:activityrule:list')")
    @GetMapping("/list")
    public AjaxResult list(RightsActivityRuleEntity rightsActivity) {
        QueryWrapper<RightsActivityRuleEntity> queryWrapper = QueryWrapperUtils.toSimpleQuery(rightsActivity);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByAsc("irasno");
        return AjaxResult.success(rightsActivityRuleService.list(queryWrapper));
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:activityrule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return success(rightsActivityRuleService.getById(id));
    }

    /**
     * 新增权益规则活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activityrule:add')")
    @Log(title = "权益规则管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RightsActivityRuleEntity rightsActivity) {
        rightsActivity.setDelFlag("0");
        rightsActivity.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsActivity.setCreateTime(new Date());
        long amt = 0;
        if(StringUtils.isNotEmpty(rightsActivity.getAmt())){
            amt = Math.round(Double.parseDouble(rightsActivity.getAmt()) * 100);
            rightsActivity.setAmt(String.valueOf(amt));
        }

        if(StringUtils.isNotEmpty(rightsActivity.getTransactionMinimum())){
            long transactionMinimum = Math.round(Double.parseDouble(rightsActivity.getTransactionMinimum()) * 100);
            if(transactionMinimum < amt){
                return AjaxResult.error("设定门槛金额不能低于券的面额");
            }
            rightsActivity.setTransactionMinimum(String.valueOf(transactionMinimum));
        }
        return toAjax(rightsActivityRuleService.save(rightsActivity));
    }
    
    /**
     * 修改权益规则活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activityrule:edit')")
    @Log(title = "权益规则管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RightsActivityRuleEntity rightsActivity) {
        RightsActivityRuleEntity queryData = rightsActivityRuleService.getById(rightsActivity.getId());
        if(queryData == null){
            return AjaxResult.success("权益活动规则不存在，无法修改!");
        }

        long amt = 0;
        if(StringUtils.isNotEmpty(rightsActivity.getAmt())){
            amt = Math.round(Double.parseDouble(rightsActivity.getAmt()) * 100);
            rightsActivity.setAmt(String.valueOf(amt));
        }

        if(StringUtils.isNotEmpty(rightsActivity.getTransactionMinimum())){
            long transactionMinimum = Math.round(Double.parseDouble(rightsActivity.getTransactionMinimum()) * 100);
            if(transactionMinimum < amt){
                return AjaxResult.error("设定门槛金额不能低于券的面额");
            }
            rightsActivity.setTransactionMinimum(String.valueOf(transactionMinimum));
        }
        rightsActivity.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsActivity.setUpdateTime(new Date());
        return toAjax(rightsActivityRuleService.updateById(rightsActivity));
    }

    /**
     * 删除权益规则活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activityrule:remove')")
    @Log(title = "权益规则管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        LambdaUpdateWrapper<RightsActivityRuleEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(RightsActivityRuleEntity::getId,ids);
        updateWrapper.set(RightsActivityRuleEntity::getDelFlag,"1");
        updateWrapper.set(RightsActivityRuleEntity::getUpdateTime,new Date());
        updateWrapper.set(RightsActivityRuleEntity::getUpdateBy,String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        return toAjax(rightsActivityRuleService.update(updateWrapper));
    }
}
